package com.ruoyi.attendance.controller;

import com.ruoyi.attendance.domain.Attendance;
import com.ruoyi.attendance.domain.AttendanceStatistics;
import com.ruoyi.attendance.service.IAttendanceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 考勤记录控制器
 */
@RestController
@RequestMapping("/attendance")
@RequiredArgsConstructor
public class AttendanceController extends BaseController {

    private final IAttendanceService attendanceService;


    /**
     * 根据用户ID查询当前登录用户的类型
     */
     @GetMapping("/getUserRoleType")
    public AjaxResult getUserRoleType() {
        // 从登录用户信息中获取用户角色类型
        SysUser User = attendanceService.getRoleType(SecurityUtils.getUserId());
         System.out.println("当前用户角色类型:" + User.getRoleType());
        return AjaxResult.success(User.getRoleType());
    }

    /**
     * 考勤记录列表查询（带分页、日期筛选）
     */
    @GetMapping("/list")
    public TableDataInfo list(Attendance attendance) {
        startPage();
        // 调用服务查询分页数据
        List<Attendance> attendanceList = attendanceService.selectAttendanceList(attendance);
        return getDataTable(attendanceList);
    }

    /**
     * 上班打卡
     */
    @PostMapping("/clockIn")
    public AjaxResult clockIn() {
        // 调用服务处理上班打卡逻辑
        Integer status = attendanceService.clockIn();
        return AjaxResult.success(status);
    }

    /**
     * 下班打卡
     */
     @PostMapping("/clockOut")
    public AjaxResult clockOut() {
        // 调用服务处理下班打卡逻辑
        Integer status = attendanceService.clockOut();
        return AjaxResult.success(status);
    }

    /**
     * 普通用户统计记录数
     */
    @GetMapping("/monthStat")
    public AjaxResult getAttendanceStatistics() {
        // 调用服务查询统计数据
        AttendanceStatistics statistics = attendanceService.selectById();
        return AjaxResult.success(statistics);
    }
}
